<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Area</title>
    <link rel="stylesheet" href="/Trees/LayUiBac/layui/css/layui.css" media="all">
</head>
<body>


<div class="layui-container">
    <div class="layui-row" style="border: cadetblue 1px solid;padding: 20px;margin-top: 30px">
        <h1>班级管理一览</h1>
        <table id="demo" lay-filter="classTable"></table>

        <div class="layui-col-md4" style="border: cadetblue 1px solid;padding: 20px">
            <input type="text" name="title" required lay-verify="required" id="areaName" placeholder="请输入要查询的区域名称"
                   autocomplete="off" class="layui-input">
            <br>

            <input type="text" name="title" required lay-verify="required" id="className" placeholder="请输入要查询的小班名称"
                   autocomplete="off" class="layui-input">
            <br>
            <button class="layui-btn" type="button" id="Find">查询</button>
            <button class="layui-btn" type="button" id="back">所有</button>
            <button class="layui-btn layui-bg-orange" type="button" id="buttonAdd">添加新的班级信息</button>
        </div>


    </div>

</div>


<script src="/Trees/LayUiBac/layui/layui.js"></script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看详情</a>
    <a class="layui-btn layui-btn-xs" lay-event="alt">修改信息</a>
</script>

<script>
    layui.use('table', function () {
        let $ = layui.$;
        let layer = layui.layer;
        let table = layui.table;
        //第一个实例
        let show = table.render({
            elem: '#demo'
            , url: '/Trees/classManage' //数据接口
            , page: true //开启分页
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "count": res.total, //解析数据长度
                    "data": res.list
                };
            }
            , cols: [[ //表头
                {field: 'pkId', title: 'ID', fixed: 'left'}
                , {field: 'className', title: '班级名称'}
                , {field: 'people', title: '负责人名称'}
                , {field: 'tel', title: '负责人联系电话'}
                , {field: 'area', title: '管理地区'}
                , {fixed: 'right',  align: 'center', toolbar: '#barDemo'}
            ]]
        });

        $('#Find').on('click', function () {
            show.reload({
                where: {
                    areaName: $('#areaName').val()
                    , className: $('#className').val()
                }
                , page: {
                    curr: 1
                }
            })
        })

        $('#back').on('click', function () {
            show.reload({
                where: {
                    areaName: ""
                    , className: ""
                }
                , page: {
                    curr: 1
                }
            })
        })


        table.on('tool(classTable)', function (obj) {
            let data = obj.data;
            console.log(data)
            let layEvent = obj.event;
            let tr = obj.tr;

            if (layEvent === "detail") {
                layer.open({
                    type: 2
                    , content: "/Trees/LayUiBac/home/disaster/Jump/showClassMsg.html"//填一个url地址
                    , area: ['600px', '600px']
                    , shade: ['0.9', '#393D49']
                    , anim: 0
                    , maxmin: true
                    , btn: ["返回主菜单"]
                    , yes: function (index, layero) {
                        layer.close(index);
                    }
                    , success: function (layero, index) {
                        let body = layer.getChildFrame('body', index);
                        let className = body.find("input[name=className]").val(data.className)
                        let people = body.find("input[name=people]").val(data.people)
                        let tel = body.find("input[name=tel]").val(data.tel)
                        let area = body.find("input[name=area]").val(data.area)
                        let num = body.find("input[name=num]").val(data.num)
                    }
                })
            }else if(layEvent==="alt"){
                layer.open({
                    type: 2
                    , content: "/Trees/LayUiBac/home/disaster/Jump/showClassMsg.html"//填一个url地址
                    , area: ['600px', '600px']
                    , shade: ['0.9', '#393D49']
                    , anim: 0
                    , maxmin: true
                    , btn: ["确认修改"]
                    , yes: function (index, layero) {
                        let body = layer.getChildFrame('body', index);
                        let people = body.find("input[name=people]").val()
                        let tel = body.find("input[name=tel]").val()
                        let num = body.find("input[name=num]").val()
                        let strs = "/Trees/classManage?method=alt"+"&people="+people+"&tel="+tel+"&num="+num+"&pkId="+data.pkId;
                        $.ajax({
                            type: "get"
                            , url: strs
                        })
                        layer.close(index);
                        show.reload();
                    }
                    , success: function (layero, index) {
                        let body = layer.getChildFrame('body', index);
                         body.find("input[name=className]").val(data.className)
                         body.find("input[name=people]").val(data.people)
                         body.find("input[name=tel]").val(data.tel)
                         body.find("input[name=area]").val(data.area)
                         body.find("input[name=num]").val(data.num)
                        // let strs = "/Trees/classManage?method=alt"+"&people="+people+"&tel="+tel+"&num="+num+"&pkId="+data.pkId;
                        // console.log(strs)
                    }
                })

            }
        })


        $('#buttonAdd').on('click', function () {
            layer.open({
                type: 2
                , content: "/Trees/LayUiBac/home/disaster/Jump/showClass.html"
                , area: ['600px', '600px']
                , shade: ['0.9', '#393D49']
                , anmi: 0
                , maxmin: true
                , btn: ['确认添加']
                , yes: function (index, layero) {
                    let body = layer.getChildFrame('body', index);
                    let className = body.find("input[name=className]").val()
                    let people = body.find("input[name=people]").val()
                    let tel = body.find("input[name=tel]").val()
                    let area = body.find("select[name=area]").val()
                    let num = body.find("input[name=num]").val()
                    let strs = "/Trees/classManage?method=add" + "&className=" + className + "&people=" + people + "&tel=" + tel + "&area=" + area+"&num="+num;
                    console.log(strs)
                    $.ajax({
                        type: "get"
                        , url: strs
                    })
                    layer.close(index);
                    show.reload();
                }
                , success: function (layero, index) {

                }
            })
        })


    });
</script>

</body>
</html>